<?php

/**
 * this class provides function for working with a tabbar
 * @author		ungerboeck simon <su@militaerversand.at>
 *
 */
class Tabbar {
    private $id = null;
    private $tabs = null;
    private $border = null;
    private $fit = null;
    private $width = null;
    private $height = null;
    private $plain = null;
    private $beforeCloseFunction = null;

    /************************************************************************
     ************************** PUBLIC FUNCTIONS ****************************
     ************************************************************************/

    /**
     * sets the id, border, fit, plain and tabs array
     * @param         string|null         $id
     */
    public function __construct($id = null) {
        $this->id = $id;
        $this->border = false;
        $this->fit = true;
        $this->plain = false;
        $this->tabs = array();
    }


    /**
     * adds a tab tu tab array
     * @param        mixed         $tab
     */
    public function addTab(Tab $tab) {
        if ($tab != null)
            array_push($this->tabs, $tab);
    }


    /**
     * creates the tabbar with all tabs, subtabs and content
     * @return         string        $output
     */
    public function create() {
        $output = '<div class="easyui-tabs" ';
        $output .= (isset($this->id) ? 'id="' . $this->id . '" ' : '');
        $output .= $this->getStyleOptions();
        $output .= $this->getDataOptions();
        $output .= '>';
        $output .= $this->createTabs();
        $output .= '</div>';
        return $output;
    }


    /**
     * converts the obj to a string output
     * @return         string
     * @see            NCSLayout
     */
    public function __toString() {
        return $this->create();
    }


    /************************************************************************
     **************************** GETTER / SETTER ***************************
     ************************************************************************/

    public function setId($id) {
        $this->id = $id;
    }


    public function setBorder($border) {
        $this->border = $border;
    }


    public function setFit($fit) {
        $this->fit = $fit;
    }


    public function setPlain($plain) {
        $this->plain = $plain;
    }


    public function setSizes($width, $height) {
        if (is_numeric($width) and is_numeric($height)) {
            $this->width = $width . 'px';
            $this->height = $height . 'px';
        } else
            throw new Exception('your given width or height is not a numeric value!');
    }
    
    
    public function setBeforeCloseFunction($function){
        $this->beforeCloseFunction = $function;
    }


    public function getId() {
        return $this->id;
    }


    public function getBorder() {
        return $this->border;
    }


    public function getFit() {
        return $this->fit;
    }


    public function getPlain() {
        return $this->plain;
    }


    public function getWidth() {
        return $this->width;
    }


    public function getHeight() {
        return $this->height;
    }


    public function getTabs() {
        return $this->tabs;
    }
    
    
    public function getBeforeCloseFunction(){
        return $this->beforeCloseFunction();
    }
    
    

    /************************************************************************
     ************************** PRIVATE FUNCTIONS ***************************
     ************************************************************************/


    /**
     * returns the style option for this datagrid
     * @return         string        $output
     */
    private function getStyleOptions() {
        $output = '';
        $output .= ((isset($this->width) or isset($this->height)) ? 'style="' . (isset($this->width) ? 'width:' . $this->width . 'px; ' : '') . (isset($this->height) ? 'height:' . $this->height . 'px' : '') . '" ' : '');
        $output .= ' ';
        return $output;
    }


    /**
     * returns the output for data options
     * @return         string        $output
     */
    private function getDataOptions() {
        $output = 'data-options="';
        $output .= (isset($this->beforeCloseFunction) ? 'onBeforeClose:' . $this->beforeCloseFunction . ', ' : '');
        $output .= (isset($this->fit) ? 'fit:' . (($this->fit) ? 'true,' : 'false,') : '');
        $output .= (isset($this->border) ? 'border:' . (($this->border) ? 'true,' : 'false,') : '');
        $output .= (isset($this->plain) ? 'plain:' . (($this->plain) ? 'true' : 'false') : '');
        $output .= '" ';
        return $output;
    }


    /**
     * creates all tabs
     * @return         string         $output   
     */
    private function createTabs() {
        $output = '';
        foreach ($this->tabs as $tab)
            $output .= $tab->create();
        return $output;
    }
}

?>